*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do



putexcel set "`outfigdir'`s'figure3_tabA3.xlsx", replace

*********************************************county full sample*********************************


foreach geo in "co" "cz" {


*********************** CONTIGUOUS COUNTIES *****************************************
drop _all
 
use "`dlrdir'`s'QCEWindustry_minwage_contig_`geo'.dta"

*TO CORRECT DRL'S STANDARD ERRORS IN COUNTY-PAIR REGRESSIONS (PAIRTIMES==2)
merge m:1 pair_id using "`dlrdir'`s'`geo'_pairs_dlr.dta"

drop if _merge==2
drop _merge

/*****  FIGURE 3 - DESCRIPTIVE *********/


/* number of counties with minimum wage gaps */

gen numpairstosum = 1/8 if year<2006
replace numpairstosum = 1/4 if year==2006

table year if lnMW_dif_period==1 & nonmissing_rest_both ==66   , c(sum numpairstosum) 

 
 
* mean minimum wage gap in counties with gap */
table year if lnMW_dif_period==1 & year<2007 & year>=1990 & nonmissing_rest_both==66     , c(mean lnMW_gap_pair )
 

 


************   residual leads/lags ***********

tsset pair_id_county period


 
forval j = 2(2)14 {
 
	gen lnMW_l`j' = S2.L`j'.lnMW 
      if `j' < 10 {
         gen lnMW_f`j' = S2.F`j'.lnMW 
       }
} 


gen lnMW_l16 = L16.lnMW
gen lnMW_l0 = S2.lnMW

gen sampleperiod = (year>=1990 & (year<2006) | (year ==2006 & quarter<3) )


forval j = 2(2)14 {
 
      xi: areg lnMW_l`j' if sampleperiod  , cluster(state_fips) absorb(countyreal)
      predict lnMWR_l`j', res
      if `j' < 10 {
         xi: areg lnMW_f`j' if sampleperiod  , cluster(state_fips) absorb(countyreal)
         predict lnMWR_f`j', res       
     }
} 

 

xi: areg lnMW_l16 if sampleperiod  , cluster(state_fips) absorb(countyreal)
      predict lnMWR_l16, res

xi: areg lnMW_l0 if sampleperiod  , cluster(state_fips) absorb(countyreal)
      predict lnMWR_l0, res


*****************************
 
  
xi: areg lnemp_rest_both if sampleperiod  , cluster(state_fips) absorb(countyreal)
predict lnemp_rest_bothR, res

xi: areg lnemp_TOT  if sampleperiod , cluster(state_fips) absorb(countyreal)
predict lnemp_TOTR, res

xi: areg lnMW if sampleperiod  , cluster(state_fips) absorb(countyreal)
predict lnMWR , res

xi:  areg lnpop  if sampleperiod  ,    absorb(county)
predict lnpopR, res
 


xi: areg lnAWW_TOT  if sampleperiod , cluster(state_fips) absorb(countyreal)
predict lnwage_TOTR, res

xi: areg lnAWW_RET  if sampleperiod , cluster(state_fips) absorb(countyreal)
predict lnwage_retR, res

xi: areg lnAWW_rest_both if sampleperiod , cluster(state_fips) absorb(countyreal)
predict lnwage_rest_bothR, res


foreach j in lnwage_rest_bothR lnwage_TOTR lnemp_rest_bothR lnemp_TOTR lnpopR lnMWR {
 xi: qui areg `j' if nonmissing_rest_both==66, a(pair_id_period)
 predict `j'R, res
}

 
egen pair_id_num = group(pair_id)
 

tsset pair_id_county period

 

 
gen sample_5 = 1
egen absorb_5 = group(pair_id period)
egen stperiod = group(state_fips period)

sort  pair_id period
 
gen state_a = real(substr(pair_id, 1,2))
gen state_b = real(substr(pair_id, 7,2))

gen st_min = min( state_a, state_b)
gen st_max = max(state_a, state_b)

egen bordersegment = group(st_min st_max)

egen bordersegmentperiod = group(bordersegment period)

gen lnMWfed = ln(federalmin)
gen statemin_2 = federalmin
replace statemin_2 = stminwage if   stminwage> federalmin & stminwage!=.

 
gen lnMW_st = max(ln(federalmin), ln(statemin_2)) 

gen lnMWst = ln( stminwage)


tsset pair_id_county period

  
 
gen lnemp_TOTR_popR = lnemp_TOTR - lnpopR

gen lnemp_TOT_pop = exp(lnemp_TOTR_popR)


*
set level 90, perm


eststo m4_`geo': xi: cluster2areg lnemp_rest_bothR lnMWR_f8 lnMWR_f6 lnMWR_f4 lnMWR_f2 lnMWR_l0 lnMWR_l2 lnMWR_l4 lnMWR_l6 lnMWR_l8 lnMWR_l10 lnMWR_l12 lnMWR_l14 lnMWR_l16 lnemp_TOTR lnpopR  if nonmissing_rest_both ==66  & pairtimes==2  , w(all) tcluster(bordersegment) fcluster(state_fips) a(pair_id_period)  
mat B = e(b)
mat V = e(V)
mat all=r(table)
mat list r(table)
forval j = 1/13 {
	mat est4_emp_`j' = [B[1,`j'], all[5,`j'],all[6,`j']]
}
 


matrix fullmat_`geo' = [est4_emp_1 ] 

forval j = 2/13 {

 matrix fullmat_`geo' = [fullmat_`geo'\  [ est4_emp_`j' ]]
}

matrix list fullmat_`geo', format(%9.7f)

}

/* Print results */

tempfile specs

#delimit ;
esttab m4_co m4_cz using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("DLR specifications (5) and (6) results -- PATHS")
	nonotes
	label
	stats(N_g N, labels("Number of pairs" "N") fmt(0 0))	
	replace;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(TableA3)


putexcel A1 = matrix(fullmat_co)
putexcel D1 = matrix(fullmat_cz)

clear all


